package CodingTest;
import javax.naming.LinkLoopException;
import java.util.*;
public class BinTreeConstruct {
    private int[] array = {1,2,3,4,5,6,7,8,9};
    private static List<Node> nodeList = null;
    private static class Node{
        Node leftChild;
        Node rightChild;
        int data;
        Node(int newData){
            leftChild = null;
            rightChild = null;
            data = newData;
        }
    }
    public void createBinTree(){
        nodeList = new LinkedList<Node>();
        //将一个数组的值依次转换为Node节点
        for(int nodeIndex = 0;nodeIndex < array.length;nodeIndex++){
            nodeList.add(new Node(array[nodeIndex]));
        }
        //对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树
        for(int parentIndex = 0;parentIndex < array.length/2-1;parentIndex++){
            //左孩子
            nodeList.get(parentIndex).leftChild = nodeList.get(parentIndex*2+1);
            //右孩子
            nodeList.get(parentIndex).rightChild = nodeList.get(parentIndex*2+2);
        }
        //最后一个父节点：移位最后一个父节点可能没有右孩子，所以单独拿出来处理
        int lastprentIndex = array.length/2 -1;
        nodeList.get(lastprentIndex).leftChild = nodeList.get(lastprentIndex*2 +1);
        //右孩子
        if(array.length%2 == 1){
            nodeList.get(lastprentIndex).rightChild = nodeList.get(lastprentIndex*2+2);
        }
    }
}
